package com.example.springboot.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.BasePageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.dto.AdminLoginDTO;
import com.example.springboot.entity.Admin;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.AdminService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ClassName:AdminServiceImpl
 * Package:com.example.springboot.service.impl
 * Description:
 *
 * @Author: Gao
 * @Create: 2023/2/2 - 11:02
 */
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    private static final String PASS_SALT = "gao";

    @Override
    public List<Admin> list() {

        return adminMapper.list();
    }

    @Override
    public Object pageInfo(BasePageRequest basePageRequest) {
        PageHelper.startPage(basePageRequest.getPageNum(), basePageRequest.getPageSize());
        List<Admin> admins = adminMapper.listByCondition(basePageRequest);
        return new PageInfo<>(admins);
    }

    @Override
    public void save(Admin admin) {
        admin.setStatus(true);
        try {
            adminMapper.save(admin);
        } catch (DuplicateKeyException e) {
            log.error("数据插入失败", admin.getAccount());
            throw new ServiceException("账号已存在");
        }

    }

    @Override
    public void deleteById(int id) {
        adminMapper.deleteById(id);
    }

    @Override
    public AdminLoginDTO login(LoginRequest request) {

        Admin admin = adminMapper.getByAccount(request.getAccount());
        //判断账号是否存在
        if (admin == null) {
            throw new ServiceException("该账号不存在");
        }
        // 判断密码是否合法
        if (!admin.isStatus()) {
            throw new ServiceException("当前账号已被禁用，请联系其他管理员");
        }
        AdminLoginDTO loginDTO = new AdminLoginDTO();
        BeanUtils.copyProperties(admin, loginDTO);

        //登录后生成token
        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword());
        loginDTO.setToken(token);
        return loginDTO;
    }

    @Override
    public void update(Admin admin) {
        admin.setUpdatetime( new Date());
        adminMapper.updateById(admin);
    }

    @Override
    public Admin getById(int id) {
        Admin admin = adminMapper.getById(id);
        return admin;
    }

    private String securePassword(String password) {
        return SecureUtil.md5(password + PASS_SALT);
    }
}
